Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support has_many through associations with composite query_constraints #47452

Merged

Conversation

nvasilevski
Copy link
Contributor

This PR adds support for has_many :items, through: :association associations defined with composite query_constraints

The change is similar to other ones. Since reflection.join_primary_key and reflection.join_foreign_key can both be Arrays
https://github.com/rails/rails/blob/4085395f41f8c9c712f39ea6bec64ae6161bfbc9/activerecord/lib/active_record/reflection.rb#L512-L515
https://github.com/rails/rails/blob/4085395f41f8c9c712f39ea6bec64ae6161bfbc9/activerecord/lib/active_record/reflection.rb#L496-L499

next_chain_scope should be ready to process them as arrays.

Then we iterate over pairis of primary & foreign keys, building Equality arel nodes and joining them with AND if we have more than one eq condition.

For tests Sharded models were extended with the Tag models that is associated with BlogPost through blog_posts_tags joining table

Given an association like:
```ruby
BlogPost.has_many :blog_post_tags,
  query_constraints: [:blog_id, :blog_post_id]
BlogPost.has_many :tags, through: :blog_post_tags
```

The `tags` association records can be queried
and the resulting JOIN query will include both `blog_id` and
`blog_post_id` in the ON clause.
@eileencodes eileencodes merged commit 3886edc into rails:main Feb 22, 2023
@eileencodes eileencodes deleted the has-many-through-with-query-constraints branch February 22, 2023 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants